import time
from xiaopy import *
from cotc import COTC

try:
    ui = xp.ui()
    region = ui.stringValue("region")
    map_name = ui.stringValue("map_name")
    round_per_recover = int(ui.stringValue("round_per_recover"))
    pause_lv50_cait = ui.boolValue("pause_lv50_cait")
    pause_lv55_cait = ui.boolValue("pause_lv55_cait")
    c = COTC(region)
except Exception as e:
    xp.toast(e)

battle_sequence = [
    # 第1回合
    {
        'skill_list': [
            [1, 1, 0, 0],
            [-1, 1, 0, 0],
            [-1, 1, 0, 0],
            [-1, 1, 0, 0],
        ],
        'all_boost': True,
    }
]

battle_sequence_fallen_cait = [
    # 第1回合
    {
        'skill_list': [
            [-1, 1, 0, 0],
            [-1, 1, 0, 0],
            [-1, 1, 0, 0],
            [-1, 1, 0, 0],
        ],
        'all_boost': True,
    },
    {
        'skill_list': [
            [10, 1, 0, 0],
            [10, 1, 0, 0],
            [10, 1, 0, 0],
            [10, 1, 0, 0],
        ],
        'all_boost': True,
    }
]

battle_sequence_sacred_cait = [
    # 第1回合
    {
        'skill_list': [
            [-1, 1, 0, 0],
            [-1, 1, 0, 0],
            [-1, 1, 0, 0],
            [-1, 1, 0, 0],
        ],
        'all_boost': True,
    },
    {
        'skill_list': [
            [10, 1, 0, 0],
            [10, 1, 0, 0],
            [10, 1, 0, 0],
            [10, 1, 0, 0],
        ],
        'all_boost': True,
    },
    {
        'skill_list': [
            [-1, 1, 0, 0],
            [-1, 1, 0, 0],
            [-1, 1, 0, 0],
            [-1, 1, 0, 0],
        ],
        'all_boost': True,
    }
]


def find_lv55_cat():
    ret = xp.findColor("#000049", "1|0|#DBFFFF", 149, 69, 151, 70)
    if ret:
        return True
    else:
        return False


def find_lv50_cat():
    # ret = xp.findColor("#C0A048", "0|1|#7F672E", 157, 56, 158, 58)
    ret = xp.findColor("#112222", "1|0|#EFCDBB", 141, 68, 143, 69)  # 70武器
    if ret:
        return True
    else:
        return False


def run_script():
    mc = c.cst.map_const[map_name]
    init_mode = mc['init_mode'] if 'init_mode' in mc else 1
    map_type = mc['map_type'] if 'map_type' in mc else 0
    town_cat = mc['town_cat'] if 'town_cat' in mc else None
    wild_cat = mc['wild_cat'] if 'wild_cat' in mc else None
    while True:
        total_encounter_count = 0
        common_encounter_count = 0
        cait_encounter_count = 0
        c.move_using_worldmap(mc['wild_coord'], wild_cat)
        c.await_black_screen_transition()
        while True:
            if total_encounter_count == 0 and init_mode >= 1:
                for index, init_obj in enumerate(mc['init_arr']):
                    init_coord = init_obj.get("init_coord")
                    init_idle = init_obj.get("init_idle")
                    post_init_coord = init_obj.get("post_init_coord")
                    if init_coord:
                        c.move_using_minimap(init_coord)
                    if init_idle:
                        time.sleep(init_idle)
                    if post_init_coord:
                        for i2, post_init_coord_item in enumerate(post_init_coord):
                            xp.tap(post_init_coord_item[0], post_init_coord_item[1])
                            time.sleep(post_init_coord_item[2])
                    if index + 1 < len(mc['init_arr']):
                        c.await_black_screen_transition()
            elif common_encounter_count >= round_per_recover:
                break
            elif total_encounter_count % 4 == 0 and init_mode >= 2:
                init_obj = mc['init_arr'][-1]
                c.move_using_minimap(init_obj['init_coord'])
                time.sleep(init_obj['init_idle'])
            c.swipe_until_black_screen(c.cst.swipe_start_coord, c.cst.swipe_end_coord)
            c.wait_battle_idling()
            time.sleep(0.5)

            if map_type == 1 and find_lv50_cat():
                if pause_lv50_cait:
                    return
                else:
                    c.flee_battle()
            elif map_type == 1 and find_lv55_cat():
                if pause_lv55_cait:
                    return
                else:
                    c.flee_battle()
            elif map_type == 2 and c.find_fallen_cait():
                c.run_preset_battle_sequence(battle_sequence_fallen_cait)
                cait_encounter_count += 1
                # return
            elif map_type == 3 and c.find_sacred_cait():
                c.run_preset_battle_sequence(battle_sequence_sacred_cait)
                cait_encounter_count += 1
                # return
            elif map_name == "corridors-of-contrition" and c.find_fallen_villain():
                c.flee_battle()
            elif map_name == "crimson-river-traverse" and c.find_fallen_thespian():
                c.flee_battle()
            else:
                # c.flee_battle()
                c.run_preset_battle_sequence(battle_sequence)
                common_encounter_count += 1
            c.tap_until_main_screen_idling(c.cst.screen_top_center_coord)
            total_encounter_count += 1

        c.move_using_worldmap(mc['town_coord'], town_cat)
        c.await_black_screen_transition()
        c.tap_inn_on_main_screen(mc['rest_town'])
        c.await_black_screen_transition()
        c.recover_in_inn(mc['rest_town'])


run_script()
